//
//  WeekFreeViewController.m
//  LOLVideoPlayer
//
//  Created by SXW on 16/3/25.
//  Copyright © 2016年 盛宣伟. All rights reserved.
//

#import "WeekFreeViewController.h"
#import "AllTableViewCell.h"
#import "AllDetailController.h"
@interface WeekFreeViewController ()

@property (nonatomic, copy) NSString *urls;

@end

@implementation WeekFreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _urls = @"http://www.demaxiya.com/app/index.php?m=news&showcount=%ld&typeid=223&__ii=865168026985233&__aa=31ebff81777ab5e3";
    [self initWork];
    
}

- (void)initWork{
    
    self.urlStr = [NSString stringWithFormat:_urls,self.pageNum];
    [self initData:^(id data) {
        
        NSDictionary *dataDic = data;
        NSArray *dataArray = dataDic[@"list"];
        for (int i = 0; i < dataArray.count; i++) {
            InfoModel *model = [InfoModel mj_objectWithKeyValues:dataArray[i]];
            [self.jsonArray addObject:model];
        }
        self.tableViewframe = CGRectMake(0, -20, APPScreenWidth, APPScreenHeight + 20 - 74 - 49);
        [self.view addSubview:self.tableView];
        [self.tableView reloadData];
        //        ccLog(@"dataArray:%@",dataArray);
    }];
}


#pragma mark - cell设置

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.jsonArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell_all";
    AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[AllTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.jsonArray.count > 0) {
        InfoModel *model = [self.jsonArray objectAtIndex:indexPath.row];
        cell.model = model;
    }
    return cell;
}

//加载更多
-(void)loadData{
    
    self.pageNum += 12;
    [self initWork];
    [self.tableView.mj_footer endRefreshing];
}

//下拉刷新
-(void)refreshData{
    
    self.pageNum = 0;
    [self.jsonArray removeAllObjects];
    [self initWork];
    [self.tableView.mj_header endRefreshing];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InfoModel *model = self.jsonArray[indexPath.row];
    AllDetailController *dt = [AllDetailController new];
    dt.ids = model.ids;
    [self.navigationController pushViewController:dt animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [AllTableViewCell getCellHeight] - 10;
}

@end
